package kin.json;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import kin.bean.system.MyEnumType;
import kin.test.Mo;
import kin.test.Us;

import com.fasterxml.jackson.core.sym.Name;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import com.fasterxml.jackson.databind.deser.impl.SetterlessProperty;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;

public class MyBeanDeserializerModifier extends BeanDeserializerModifier{

	
	
/*	@Override
	public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
			BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
		List<BeanPropertyDefinition> properties = beanDesc.findProperties();
	    for(BeanPropertyDefinition property: properties) {
	        String name = property.getName();
	        
	        // has "feature"? => It's an ExtendedOptions object
	        System.out.println(name);
	        if("parent".equals(name)) {
	            System.out.println("It's an extended object!");
	           // BeanDeserializerBuilder bdb=  new   BeanDeserializerBuilder(beanDesc, config).;
	           return deserializer;
	            //return deserializer.replaceDelegatee(new CustomerDeserializer());
	        }
	        if("children".equals(name)){
	        	
	        }
	    }
	    System.out.println("It's not an extended object!");
	    return super.modifyDeserializer(config, beanDesc, deserializer);
	
		//return super.modifyDeserializer(config, beanDesc, deserializer);
	}
	*/
	
	
	
	/*  @Override
      public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BasicBeanDescription beanDesc, BeanDeserializerBuilder builder) {
          Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
          while (beanPropertyIterator.hasNext()) {
              SettableBeanProperty settableBeanProperty = beanPropertyIterator.next();
              if (PropertyName.equals(settableBeanProperty.getName())) {
                  SettableBeanProperty newSettableBeanProperty = settableBeanProperty.withValueDeserializer(new CustomDeserializer());
                  builder.addOrReplaceProperty(newSettableBeanProperty, true);
                  break;
              }
          }
          return builder;
      }*/
	
/*
	@Override
	public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc,List<BeanPropertyDefinition> propDefs) {
		
		for(BeanPropertyDefinition bpd:propDefs){
			System.out.println("type:"+bpd.getGetter().getRawReturnType());
			if(bpd.getGetter().getRawReturnType()==Us.class){
				
			
				SettableBeanProperty prop = new SetterlessProperty(beanDesc, beanDesc.getType(), typeDeser,
		                beanDesc.getClassAnnotations(), beanDesc.findAnyGetter());
				System.out.println("name:"+bpd.getName());
			  
			}
			
		}
		
		
		/*List<BeanPropertyDefinition> properties = beanDesc.findProperties();
		
	    for(BeanPropertyDefinition property: properties) {
	        String name = property.getName();
	        
	        // has "feature"? => It's an ExtendedOptions object
	        System.out.println(name);
	        if("parent".equals(name)) {
	            System.out.println("It's an extended object!");
	        
	         
	        }
	        if("children".equals(name)){
	        	
	        }
	    }*/
	
         /*   CollectionType type = CollectionType.construct(ArrayList.class, SimpleType.construct(Member.class));
            TypeDeserializer typeDeserializer = type.getTypeHandler();
            SettableBeanProperty.MethodProperty membersProperty = (SettableBeanProperty.MethodProperty) builder.removeProperty("members");
            builder.addProperty(new SettableBeanProperty.MethodProperty(
               "members",
               type,
               typeDeserializer,
               beanDesc.getClassAnnotations(),
               (AnnotatedMethod) membersProperty.getMember()
            ));
          

		return super.updateProperties(config, beanDesc, propDefs);
	}*/

	/*@Override
	public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
			BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
		if(beanDesc.getBeanClass()==Us.class){
			System.out.println("oooooooo");
			return new CustomerDeserializer();
		}
		return  deserializer;
	}*/

	@Override
	public BeanDeserializerBuilder updateBuilder(DeserializationConfig config,
			BeanDescription beanDesc, BeanDeserializerBuilder builder) {
	
		Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
        while (beanPropertyIterator.hasNext()) {
            SettableBeanProperty settableBeanProperty = beanPropertyIterator.next();
         
        
          Class cls=settableBeanProperty.getType().getRawClass();
          if(!cls.isPrimitive()){
        	  System.out.println("class------:"+ cls);
        	  //cls.getPackage().getName();
        	 
           if("parent".equals(settableBeanProperty.getName())||Mo.class==cls||Us.class==cls||cls==List.class||cls==Set.class||"children".equals(settableBeanProperty.getName())){
        	   //settableBeanProperty.
        	   System.out.println("ddddppp:"+ settableBeanProperty.getName());
        	  // settableBeanProperty.setManagedReferenceName("parent");
                SettableBeanProperty newSettableBeanProperty = settableBeanProperty.withValueDeserializer(new CustomerDeserializer());
               // settableBeanProperty.
               // ObjectIdReader r = null;
				//newSettableBeanProperty.setManagedReferenceName("parent");
               // builder.setObjectIdReader(r);
                builder.addOrReplaceProperty(newSettableBeanProperty, true);
                //break;
           }
        }
        }
		return super.updateBuilder(config, beanDesc, builder);
	}

}
